package com.glamour.hlcity.module.dom.analysis.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.glamour.hlcity.common.model.Node;
import com.glamour.hlcity.common.util.ListUtil;
import com.glamour.hlcity.module.dom.analysis.TreeOverlap;

public abstract class AbstractTreeOverlap implements TreeOverlap {

	@Override
	public Node overlap(Node mTree, Node cTree) {
		
		
		return null;
	}
	
	protected void addNode(List<Node> mNodeList,Node cNode){
		if(mNodeList.isEmpty()){
			return;
		}
		Node mNode=filterSameNode(mNodeList, cNode);
		if(mNode==null){
			ListUtil.first(mNodeList).getParentNode().addChildNode(cNode);
		}
	}
	
	/**
	 * 判断两个节点是否可以重叠(默认这两个几点层级是一样的)
	 * @param mNode
	 * @param cNode
	 * @return
	 */
	abstract boolean sameNode(Node mNode,Node cNode);
	
	/**
	 * 过滤所有的节点获得可以重叠的主干节点
	 * @param mNodeList
	 * @param cNode
	 * @return
	 */
	final protected Node filterSameNode(List<Node> mNodeList,Node cNode){
		for(Node mNode:mNodeList){
			if(sameNode(mNode, cNode)){
				return mNode;
			}
		}
		return null;
	}
	
	//基本的参数校验
	final protected boolean valid(Node mNode, Node cNode){
		if(mNode==null || cNode==null){
			return false;
		}
		//如果两个都不为Tag标签
		if(!(Node.HTML_ELEMENT_TAG.equals(mNode.getNodeType())&&Node.HTML_ELEMENT_TAG.equals(cNode.getNodeType()))){
			return false;
		}
		if(StringUtils.isBlank(mNode.getNodeNo())|| StringUtils.isBlank(cNode.getNodeNo())){
			return false;
		}
		return true;
	}

}
